﻿/**
 * Discription: 
 * File Name:   $ItemName$.cs
 * Author   :   陈小磊(Gtalk/Email:chenlei@hzexe.com)
 * Create Time: 2013/12/17 11:16:41
 * CLR Version: 4.0.30319.18052
 * File Identification: 61654031-1ad9-49ea-9ac3-330529b7f506
 * Create by wpc\machinename in project named "$ProjectName$" 
 * =================================================
 * Modify Records
 *          
 * 
 * 
 * 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Hzexe.Wechat.SharpSDK.Message.JSONData.Support
{
    /// <summary>
    /// 发送客服消息结果
    /// </summary>
    public class PushMessageResult : ResultBase
    {
        /// <summary>
        /// 发送客服消息
        /// </summary>
        /// <param name="message">要推送的消息</param>
        /// <returns>消息发送结果</returns>
        public static new PushMessageResult GetResult<T>(Hzexe.Wechat.SharpSDK.Message.Push.PushMessage<T> message)
        {
            var url = string.Format(
                @"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}",
                Support.AccessTokenResult.AccessToken
                );

            var poststr = message.ToString();

            string jsonstring = @"{""errcode"":40013,""errmsg"":""http request error""}";
            WebClient wc = new WebClient();
            wc.Encoding = System.Text.Encoding.UTF8;
            try
            {
                jsonstring = wc.UploadString(url, null, poststr);
            }
            catch (Exception ex)
            {
                log.Warn("请求远程服务器" + url + "失败", ex);
            }
            finally
            {
                wc.Dispose();
            }

            return Newtonsoft.Json.JsonConvert.DeserializeObject<PushMessageResult>(jsonstring);
        }
    }
}
